package com.ll.beautymall.utils;

import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;

/**
 * Created by Administrator on 2016/10/25.
 */
public class CommonUtil {

    public static HashMap<String, String> getParameters(String result) {
        HashMap<String, String> parameters = new HashMap<>();
        String[] split = result.split("&");
        //不接 title
        for (int i = 0; i < split.length - 1; i++) {
            String[] para = split[i].split("=");
            if (para[0].equals("title")) {
                continue;
            }
            parameters.put(para[0], para[1]);
        }
        return parameters;
    }

    public static String getTitle(String result) {
        String[] split = result.split("&");
        for (int i = 0; i < split.length; i++) {
            String[] para = split[i].split("=");
            if (para[0].equals("title")) {
                try {
                    String title = URLDecoder.decode(para[1], "UTF-8");
                    return title;
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public static void configWebview(WebView webView, String url) {
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setBuiltInZoomControls(true);

        webView.loadUrl(url);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
    }
}
